package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.BrandAddNewParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.BrandService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/brands")
@Slf4j
@Api(tags = "08. 品牌管理模块")
@Validated
public class BrandController {

    @Autowired
    private BrandService service;

    @PostMapping("/add-new")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid BrandAddNewParam brandAddNewParam){
        service.addNew(brandAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult delete(
            @Range(min = 1,message = "品牌id必须大于0")
            @RequestParam Long id){
        log.debug("开始处理【根据ID删除品牌】的请求,参数: {}",id);
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改品牌")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult updateBrand(
            @Range(min = 1,message = "品牌id必须大于0")
            @RequestParam Long id, @Valid BrandUpdateInfoParam brandUpdateInfoParam){
        service.updateBrand(id,brandUpdateInfoParam);
        return JsonResult.ok();
    }


    // http://localhost:9180/brand/standard
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询相册详情失败，请提交合法的ID值！")
                                      @PathVariable Long id) {
        log.debug("开始处理【根据ID查询相册详情】的请求,参数: {}",id);
        BrandStandardVo result = service.getStandardById(id);
        return JsonResult.ok(result);
    }


    // http://localhost:9180/brand/list
    @GetMapping("")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query"),
            @ApiImplicitParam(name = "queryType",value = "查询类型",example = "all")
    })
    public JsonResult list(@Range(min = 1,message = "查询相册列表失败，请提供正确的页码值!") Integer page,
                           String queryType) {
        log.debug("开始处理【查询品牌列表】的请求，页码:{}",page);
        if (page==null || page<1){
            page=1;
        }
        PageData<BrandListItemVo> pageData = null;
        if ("all".equals(queryType)){
            pageData = service.list(page,Integer.MAX_VALUE);
        } else {
            pageData = service.list(page);
        }
        return JsonResult.ok(pageData);
    }
}
